#include "switchbuttoninside.h"
#include <QPainter>
#include <QFont>
#include <QPainterPath>
#include <QPropertyAnimation>
#include "slider.h"
SwitchButtonInside::SwitchButtonInside(QWidget *parent)
    : QWidget{parent}, m_nMargin(2),m_nDuration(100)
{
    resize(72, 28); // 默认80，28宽高
    m_pSlider = new Slider(this);
    m_pSlider->resize(height() - m_nMargin * 2, height() - m_nMargin * 2);
    m_pSlider->move(m_nMargin, m_nMargin);
    m_bActive = false; // 默认未激活
    m_nArcRadius = std::min(width(), height()); // 默认半径
    m_nRectWidth = width() - m_nArcRadius;
    m_colorActive = qRgb(60, 189, 136);
    m_colorInactive = qRgb(167, 177, 188);

}
/**
 * @brief SwitchButtonInside::SetSize
 * @param nWidth
 * @param nHeight
 * m_pSlider->move(m_nMargin, m_nMargin); 有点问题，当改变窗口尺寸大小
 */
void SwitchButtonInside::SetSize(int nWidth, int nHeight)
{
    resize(nWidth, nHeight);
    m_pSlider->resize(height() - m_nMargin * 2, height() - m_nMargin * 2);
    m_pSlider->move(m_nMargin, m_nMargin);
    m_nArcRadius = std::min(width(), height());
    m_nRectWidth = width() - m_nArcRadius;

}

void SwitchButtonInside::SetActiveColor(const QColor &color)
{
    m_colorActive = color;
}

void SwitchButtonInside::SetInactiveColor(const QColor &color)
{
    m_colorInactive = color;
}

void SwitchButtonInside::SetSliderColor(const QColor &color)
{
    m_pSlider->SetSliderColor(color);
}

void SwitchButtonInside::SetStatus(bool bActive)
{
    if(m_bActive == bActive) {
        return;
    }
    m_bActive = bActive;
    if(m_bActive) {
        ToActive();
    } else {
        ToInactive();
    }

}

bool SwitchButtonInside::GetStatus() const
{
    return m_bActive;
}

void SwitchButtonInside::SetText(const QString &text)
{
    m_text = text;
}

void SwitchButtonInside::paintEvent(QPaintEvent *event)
{
    qDebug() << "[SwitchButtonInside]m_nArcRadius = " << m_nArcRadius
             << "| m_nRectWidth << " << m_nRectWidth
             << "| size = " << width() << "," << height();

    if (m_nArcRadius > height()) {
        qDebug() << "******* switchbutton resize ******";
        SetSize(width(), height());
    }

    QPainter p;
    p.begin(this);
    p.setRenderHint(QPainter::Antialiasing, true);
    p.setPen(Qt::NoPen);
    if(m_bActive) p.setBrush(QBrush(m_colorActive));
    else p.setBrush(QBrush(m_colorInactive));

    QPainterPath leftPath;
    leftPath.addEllipse(0, 0, m_nArcRadius, m_nArcRadius);

    QPainterPath middlePath;
    middlePath.addRect(m_nArcRadius / 2, 0, m_nRectWidth, m_nArcRadius);

    QPainterPath rightPath;
    rightPath.addEllipse(m_nRectWidth, 0, m_nArcRadius, m_nArcRadius);

    QPainterPath path = leftPath + middlePath + rightPath;

    p.drawPath(path);

    QPen pen;
    pen.setColor(Qt::white);
    p.setPen(pen);
    QFont ft;
    ft.setPointSize(9);
    p.setFont(ft);
    if (m_bActive) {
        p.drawText(QRect(0, 0, m_nRectWidth,
                         m_nArcRadius), Qt::AlignCenter, m_text);
    } else {
        p.drawText(QRect(m_nArcRadius, 0,
                         m_nRectWidth, m_nArcRadius), Qt::AlignCenter, m_text);
    }


    p.end();

}

void SwitchButtonInside::mousePressEvent(QMouseEvent *event)
{
    QWidget::mousePressEvent(event);
}

void SwitchButtonInside::mouseReleaseEvent(QMouseEvent *event)
{
    emit Clicked(!m_bActive);
    QWidget::mouseReleaseEvent(event);
}

void SwitchButtonInside::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    // 更新尺寸
    SetSize(width(), height());
}

void SwitchButtonInside::ToActive()
{
    QPropertyAnimation* pAnimation = new QPropertyAnimation(m_pSlider, "geometry");
    pAnimation->setDuration(m_nDuration);
    pAnimation->setStartValue(m_pSlider->rect());
    pAnimation->setEndValue(QRect(width() - m_pSlider->width() - m_nMargin,
                                  m_nMargin,
                                  m_pSlider->width(),
                                  m_pSlider->height()));
    connect(pAnimation, &QPropertyAnimation::valueChanged, this, [&](const QVariant &value){
        Q_UNUSED(value)
        update();
    });
    pAnimation->start(QAbstractAnimation::DeleteWhenStopped);

}

void SwitchButtonInside::ToInactive()
{
    QPropertyAnimation* pAnimation = new QPropertyAnimation(m_pSlider, "geometry");
    pAnimation->setDuration(m_nDuration);
    pAnimation->setStartValue(QRect(m_pSlider->x(),
                                    m_pSlider->y(),
                                    m_pSlider->width(),
                                    m_pSlider->height()));
    pAnimation->setEndValue(QRect(m_nMargin,
                                  m_nMargin,
                                  m_pSlider->width(),
                                  m_pSlider->height()));
    connect(pAnimation, &QPropertyAnimation::valueChanged, this, [&](const QVariant &value){
        Q_UNUSED(value)
        update();
    });
    pAnimation->start(QAbstractAnimation::DeleteWhenStopped);

}
